package com.ray.eye.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.ray.base.Constants;
import com.ray.base.adapter.CommonRecyclerAdapter;
import com.ray.base.adapter.MultiTypeSupport;
import com.ray.base.adapter.ViewHolder;
import com.ray.base.base.BaseFragment;
import com.ray.base.bean.video.AllRec;
import com.ray.base.router.RouterActivityPath;
import com.ray.base.router.RouterFragmentPath;
import com.ray.base.utils.StringUtils;
import com.ray.base.utils.TimeUtils;
import com.ray.eye.R;
import com.ray.eye.databinding.FragmentRankBinding;
import com.ray.eye.viewmodel.RankFragmentViewModel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 *总排行
 **/
@Route(path = RouterFragmentPath.Video.PAGER_HISTORICAL)
public class HistoricalFragment extends BaseFragment<FragmentRankBinding, RankFragmentViewModel> {

    private int start = 0;
    private int num = 10;
    private List<AllRec.ItemListBeanX> mList = new ArrayList<>();
    private CommonRecyclerAdapter<AllRec.ItemListBeanX> adapter;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_rank;
    }

    @Override
    public void loadData() {
        requestHistorical();
        initAdapter();
        binding.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                start += 10;
                requestHistorical();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                start = 0;
                requestHistorical();
            }
        });
    }

    private void requestHistorical() {
        viewModel.requestHistorical(start, num).observe(this, new Observer<AllRec>() {
            @Override
            public void onChanged(AllRec allRec) {
                binding.refreshLayout.finishLoadMore();
                binding.refreshLayout.finishRefresh();
                if (allRec != null) {
                    if (start == 0) {
                        mList.clear();
                    }
                    mList.addAll(allRec.getItemList());
                    adapter.notifyDataSetChanged();
                } else {
                    binding.refreshLayout.finishLoadMoreWithNoMoreData();
                }
            }
        });
    }

    private void initAdapter() {
        MultiTypeSupport multiTypeSupport = new MultiTypeSupport() {
            @Override
            public int getLayoutId(Object item, int position) {
                if (mList.get(position).getData() != null) {
                    switch (mList.get(position).getData().getDataType()) {
                        case Constants.DataType.VideoBeanForClient:
                            return R.layout.item_follow_card;
                        default:
                            return R.layout.item_horizontal_scroll_card;
                    }
                } else {
                    return R.layout.item_horizontal_scroll_card;
                }
            }
        };

        if (adapter == null) {
            adapter = new CommonRecyclerAdapter<AllRec.ItemListBeanX>(getContext(), mList, multiTypeSupport) {
                @Override
                public void convert(ViewHolder holder, final AllRec.ItemListBeanX item, int position) {
                    switch (item.getData().getDataType()) {
                        case Constants.DataType.VideoBeanForClient:
                            if (item.getData() != null) {
                                if (item.getData().getCover() != null) {
                                    //封面
                                    holder.setImageByGlide(R.id.detail, item.getData().getCover().getDetail());
                                }
                                //标题
                                holder.setText(R.id.title, item.getData().getTitle());
                                //类型
                                holder.setText(R.id.slogan, "#" + item.getData().getCategory());
                                //时长
                                holder.setText(R.id.duration, TimeUtils.formatVideoTime((long) item.getData().getDuration()));
                                if (item.getData().getAuthor() != null) {
                                    //作者头像
                                    holder.setCircleByGlide(R.id.icon, item.getData().getAuthor().getIcon());
                                }
                            }
                            break;
                    }
                    holder.setOnIntemClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (item.getData() != null && !StringUtils.isEmpty(item.getData().getPlayUrl())) {
                                jumpVideoPlay(item.getData().getPlayUrl(), item.getData().getTitle(), item.getData().getId(), item.getData().getDescription(),item.getData().getDuration(),item.getData().getCategory(),item.getData().getCover().getDetail());
                            }
                        }
                    });
                }
            };
        }
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.recyclerView.setAdapter(adapter);
    }

    private void jumpVideoPlay(String url, String title, String id, String desc, long duration, String category, String detail) {
        ARouter.getInstance()
                .build(RouterActivityPath.EyeVideo.PAGER_PLAY)
                .withString("url", url)
                .withString("title", title)
                .withString("id", id)
                .withString("desc", desc)
                .withLong("duration", duration)
                .withString("category", category)
                .withString("detail", detail)
                .navigation();
    }


}
